/*
 * Copyright 2013 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.madding.shared.encrypt.cert.bc.util;

import java.io.PrintWriter;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.asn1.DERBMPString;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
import org.bouncycastle.openssl.PEMWriter;
import org.bouncycastle.x509.extension.SubjectKeyIdentifierStructure;

import com.Ostermiller.util.CircularByteBuffer;
import com.madding.shared.encrypt.cert.bc.constant.BCConstant;
import com.madding.shared.encrypt.constants.Charset;
import com.madding.shared.encrypt.io.StreamUtil;

/**
 * 类KeyStoreUtil.java的实现描述：TODO 类实现描述
 * 
 * @author madding.lip Dec 7, 2013 12:09:41 AM
 */
public class KeyStoreUtil implements BCConstant {

    public static String getCrtFile(Certificate cert) throws Exception {
        CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
        PEMWriter pemWriter = new PEMWriter(new PrintWriter(cbb.getOutputStream()));
        pemWriter.writeObject(cert);
        pemWriter.flush();
        pemWriter.close();
        return StreamUtil.stream2Bytes(cbb.getInputStream(), Charset.UTF_8);
    }

    public static String getKeyFile(PrivateKey privKey) throws Exception {
        CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
        PEMWriter pemWriter2 = new PEMWriter(new PrintWriter(cbb.getOutputStream()));
        pemWriter2.writeObject(privKey);
        pemWriter2.flush();
        pemWriter2.close();
        return StreamUtil.stream2Bytes(cbb.getInputStream(), Charset.UTF_8);
    }
    
    public static String getP12File(KeyStore keyStore, char[] passwd) throws Exception {
        CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
        keyStore.store(cbb.getOutputStream(), passwd);
        return Base64.encodeBase64String(StreamUtil.stream2Bytes(cbb.getInputStream()));
    }

    public static KeyStore getPKCS12KeyStore(String alias, Certificate[] certChain, KeyPair keyPair) throws Exception {

        PKCS12BagAttributeCarrier bagAttr = (PKCS12BagAttributeCarrier) keyPair.getPrivate();
        bagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString(alias));
        bagAttr.setBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId,
                                new SubjectKeyIdentifierStructure(keyPair.getPublic()));
        KeyStore store = KeyStore.getInstance(KEY_STORE_TYPE, JCE_PROVIDER);
        store.load(null, null);
        store.setKeyEntry(alias, keyPair.getPrivate(), null, certChain);
        return store;
    }
}
